<?php
class Training_Db2Controller extends Zendvn_Controller_Action{
	
	protected $_arrParam;
	
	public function init(){
		$this->_arrParam = $this->_request->getParams();
	}
	public function indexAction(){
		$tblUser = new Training_Model_User();
		$db = $tblUser->getAdapter();
		
		// FROM
		/*$select = $db->select()
					 ->from('user_group AS g',array('id','name AS group_name','avatar'));*/
		
		//WHERE		
	/*	$select = $db->select()
					 ->from('user_group AS g',array('id','name','avatar'))
					 ->where('group_acp	= ?',$this->_arrParam['acp'],3); // null, 3 = string; 0,1 = INT; 2 = FLOAT*/
		
		//AND
		/*$select = $db->select()
					 ->from('user_group AS g',array('id','name','avatar'))
					 ->where('group_acp	= ?',$this->_arrParam['acp'],1)
					 ->where('group_default	= ?',$this->_arrParam['default'],1);*/
		
		//OR
		/*$select = $db->select()
					 ->from('user_group AS g',array('id','name','avatar'))
					 ->where('group_acp	= ?',$this->_arrParam['acp'],1)
					 ->orWhere('group_default = ?',$this->_arrParam['default'],1);*/
		
		//ORDER
		/*$select = $db->select()
					 ->from('user_group AS g',array('id','name','avatar'))
					 ->where('group_acp	= ?',$this->_arrParam['acp'],1)
					 ->order('id DESC');*/

		//LIMIT
		/*$select = $db->select()
					 ->from('user_group AS g',array('id','name','avatar'))
					 ->where('group_acp	= ?',$this->_arrParam['acp'],1)
					 ->order('id DESC')
					 ->limit(5,2);*/

		//LIMIT PAGE
		/*$page = $this->_request->getParam('page',1);
		$select = $db->select()
					 ->from('user_group AS g',array('id','name','avatar'))
					 //->where('group_acp	= ?',$this->_arrParam['acp'],1)
					 ->order('id DESC')
					 ->limitPage($page, 5);
*/
		//JOIN
		/*$select = $db->select()
					 ->from('users AS u',array('id','user_name','email','group_id'))
					 ->join('user_group AS g', 'g.id = u.group_id',array('name AS group_name'))
					 ->order('id DESC');*/

		//JOIN LEFT
		/*$select = $db->select()
					 ->from('users AS u',array('id','user_name','email','group_id'))
					 ->joinLeft('user_group AS g', 'g.id = u.group_id',array('name AS group_name'))
					 ->order('id ASC');*/
		//WHERE IN
		/*$groups = $this->_arrParam['groups'];
		$select = $db->select()
					 ->from('users AS u',array('id','user_name','email','group_id'))
					 ->where('id IN (' . $groups . ')');	*/

		//GROUP BY		
		/*$select = $db->select()
 					 ->from('user_group AS g',array('id','name'))
 					 ->joinLeft('users AS u','u.group_id = g.id',array('COUNT(u.id) AS total'))
 					 ->group('g.id')
 					 ->having('total > 0');*/

		//RESET
		$select = $db->select()
					 ->from('users AS u',array('id','user_name','email','group_id'))
					 ->joinLeft('user_group AS g', 'g.id = u.group_id',array('name AS group_name'))
					 ->order('id ASC');
		echo '<br>' . $select;
			
		$select->reset(Zend_Db_Select::ORDER);
		echo '<br>' . $select;
		
		$select->order('user_name DESC');					 
		echo '<br>' . $select;
		
		$result = $db->fetchAll($select);
		echo '<pre>';
		print_r($result);
		echo '</pre>';
		
		$this->_helper->viewRenderer->setNoRender();
	}
	
	

}